<?php
//View Privacy Checker
require_once APPLICATION_PATH."/modules/event/lib/Sns.php";
class Helper_Privacy extends Zend_Controller_Action_Helper_Abstract{
	
	private $_ctrl=null;
	public function setCtrl($ctrl){
		$this->_ctrl = $ctrl;
	}
	private function getOtherHelper($helperName){
		return Zend_Controller_Action_HelperBroker::getStaticHelper($helperName);
	}
	
	// can view
	public function isCanView($type, $event){
		
		if(!$this->isEnable($type, $event))
			return false;
		
		return true;
	}
	// can add
	public function isCanAdd($type, $eid, $uid){
		if(empty($uid))
			return false;
		
		$modelLoader = $this->getOtherHelper('modelLoader');
		if($this->_ctrl){
			$modelLoader->setCtrl($this->_ctrl);
		}
		
		$event = $modelLoader->getEventInfo($eid);
		
		if(!$this->isCanView($type, $event)){
			return false;
		}
		$event->$type = (int) $event->$type;
		
		if(defined('EVENT_PRIVACY_'.strtoupper($type)."_EVERYONE") && $event->$type == constant('EVENT_PRIVACY_'.strtoupper($type)."_EVERYONE"))
			return true;
		
		if(defined('EVENT_PRIVACY_'.strtoupper($type)."_PUBLIC") && $event->$type == constant('EVENT_PRIVACY_'.strtoupper($type)."_PUBLIC")){
			if($modelLoader->isEventMember($event->eid, $uid))
				return true;
		}
		
		
		if(defined('EVENT_PRIVACY_'.strtoupper($type)."_PRIVATE") && $event->$type == constant('EVENT_PRIVACY_'.strtoupper($type)."_PRIVATE")){
			if($modelLoader->isEventAdmin($event->eid, $uid))
				return true;
		}
		
		return false;
	}
	
	// type is blog, photo, video, comment, bookmark, forum, music
	public function isEnable($type, $event){
		if(empty($event))
			return false;
		if(empty($type))
			return false;
		if(is_numeric($event)){
			$modelLoader = $this->getOtherHelper('modelLoader');
			if($this->_ctrl){
				$modelLoader->setCtrl($this->_ctrl);
			}
			
			$event = $modelLoader->getEventInfo($event);
		}
		if(isset($event->$type) && $event->$type != constant('EVENT_PRIVACY_'.strtoupper($type)."_DISABLED"))
			return true;
		
		return false;
	}
	
	public function isEventSecure($access){
		return $access == EVENT_ACCESS_SECURE;
	}
	
	public function isEventPublic($access){
		return $access == EVENT_ACCESS_PUBLIC;
	}
	
	public function isEventPrivate($access){
		return $access == EVENT_ACCESS_PRIVATE;
	}
    //va
    public function isEventPremium($access){
        return $access == EVENT_ACCESS_TICKET;
    }
    //va
}

